package dbs

import (
	"DCOS-API/config"
	"DCOS-API/utils"
	"fmt"
	"github.com/go-redis/redis"
	"time"
)

var (
	rds *redis.Client
)

func Redis() *redis.Client {
	if rds == nil {
		var err error
		conf := config.GetConfig()
		redisConf := conf.Redis
		if redisConf.Password != "" {
			redisConf.Password, err = utils.AesDe(redisConf.Password, conf.BaseConfig.SignKey)
			if err != nil {
				panic(fmt.Errorf("解密mysql密码(%v)错误！", redisConf.Password))
			}
		}
		rds = redis.NewClient(&redis.Options{
			Addr:         redisConf.Addr,
			Password:     redisConf.Password,
			DB:           redisConf.DB,
			MinIdleConns: redisConf.Idle,
			MaxConnAge:   time.Second * time.Duration(redisConf.MaxConnAge),
			DialTimeout:  time.Second * time.Duration(redisConf.DialTimeout),
			ReadTimeout:  time.Second * time.Duration(redisConf.ReadTimeout),
			WriteTimeout: time.Second * time.Duration(redisConf.WriteTimeout),
			IdleTimeout:  time.Second * time.Duration(redisConf.IdleTimeout),
		})
		err = rds.Ping().Err()
		if err != nil {
			panic(err)
		}
	}
	return rds
}
